package blbetter.visual;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import blbetter.db.BLDataBase;

public class BLFrame extends JFrame implements ActionListener {

	private static final long serialVersionUID = -4309094897927057296L;

	public BLDataBase db;

	public BLSpieltagResultsPanel resultsPane;

	public BLTabellePanel tabellePane;

	public JComboBox saisonsCB;

	public JComboBox spieltageCB = new JComboBox();

	private int saisonId;

	public BLFrame(BLDataBase db) {
		super("Bundesliga");
		this.db = db;
		this.resultsPane = new BLSpieltagResultsPanel(db);
		this.tabellePane = new BLTabellePanel(db);

		JPanel contentPane = new JPanel(new GridBagLayout());

		saisonsCB = new JComboBox(db.getSaisons());

		setSpieltage();
		setData();

		saisonsCB.addActionListener(this);
		spieltageCB.addActionListener(this);

		contentPane.add(new JLabel("Saison: "), new GridBagConstraints(0, 0, 1,
				1, 0, 0, GridBagConstraints.NORTH, GridBagConstraints.VERTICAL,
				new Insets(10, 10, 0, 0), 0, 0));
		contentPane.add(saisonsCB, new GridBagConstraints(1, 0, 1, 1, 0, 0,
				GridBagConstraints.NORTH, GridBagConstraints.VERTICAL,
				new Insets(10, 10, 0, 0), 0, 0));
		contentPane.add(new JLabel("Spieltag: "), new GridBagConstraints(2, 0,
				1, 1, 0, 0, GridBagConstraints.NORTH,
				GridBagConstraints.VERTICAL, new Insets(10, 10, 0, 0), 0, 0));
		contentPane.add(spieltageCB, new GridBagConstraints(3, 0, 1, 1, 0, 0,
				GridBagConstraints.NORTH, GridBagConstraints.VERTICAL,
				new Insets(10, 10, 0, 0), 0, 0));

		// results
		contentPane.add(resultsPane, new GridBagConstraints(0, 1, 5, 1, 0, 0,
				GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(
						10, 10, 0, 10), 0, 0));
		// tabelle
		contentPane.add(tabellePane, new GridBagConstraints(0, 2, 5, 1, 0, 0,
				GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(
						10, 10, 10, 10), 0, 0));

		this.setContentPane(contentPane);
		// this.setPreferredSize(new Dimension(800, 600));
		this.pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource().equals(saisonsCB)) {
			setSpieltage();
			setData();
		}
		if (ae.getSource().equals(spieltageCB)) {
			if (spieltageCB.getSelectedIndex() >= 0) {
				setData();
			}
		}
	}

	private void setData() {
		// System.out.println(spieltageCB.getSelectedIndex());
		resultsPane.setData(saisonId, (Integer) spieltageCB.getSelectedItem());
		tabellePane.setData(saisonId, (Integer) spieltageCB.getSelectedItem());
		this.pack();
	}

	private void setSpieltage() {
		String saisonsStr = saisonsCB.getSelectedItem().toString();
		System.out.println(saisonsStr);

		int jahr1 = Integer.parseInt(saisonsStr.substring(0, saisonsStr
				.indexOf('/')));

		saisonId = db.getSaisonIdByJahr1(jahr1);

		Vector<Integer> spieltageVec = new Vector<Integer>();

		for (int i = 1; i <= 34; i++) {
			if (!db.getSpieleBySaisonIdAndSpieltag(saisonId, i).isEmpty()) {
				spieltageVec.add(i);
			} else {
				break;
			}
		}
		Collections.reverse(spieltageVec);
		// System.out.println(spieltageVec.size() + ":" + spieltageVec);
		int selectedIndex = spieltageCB.getSelectedIndex();
		if (selectedIndex < 0)
			selectedIndex = 0;
		int itemCountOld = spieltageCB.getItemCount();
		System.out.println("Oldsi: " + selectedIndex);
		spieltageCB.removeAllItems();
		for (int i = 0; i < spieltageVec.size(); i++) {
			spieltageCB.addItem(spieltageVec.get(i));
		}
		int itemCountDiff = spieltageCB.getItemCount() - itemCountOld;
		int newSelected = selectedIndex + itemCountDiff;
		if (newSelected >= spieltageCB.getItemCount())
			newSelected = 0;
		spieltageCB.setSelectedIndex(newSelected > 0 ? newSelected : 0);

		System.out.println("Newsi: " + spieltageCB.getSelectedIndex());
	}

}
